package fudan.se.project.service.infrastructure.base;

import fudan.se.project.domain.User;
import fudan.se.project.domain.conference.post.Post;
import fudan.se.project.repository.entity.conference.post.PostRepository;
import fudan.se.project.repository.entity.conference.role.PcMemberRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class PostPermissionService {

    private final PcMemberRepository pcMemberRepository;
    private final PostRepository postRepository;

    private static final  String PCMEMBER = "PCMEMBER";

    @Autowired
    public PostPermissionService(PcMemberRepository pcMemberRepository, PostRepository postRepository) {
        this.pcMemberRepository = pcMemberRepository;
        this.postRepository = postRepository;
    }

    public boolean checkPermission(User user, String authority, long resourceId) {
        Post post = postRepository.findById(resourceId);
        if (post == null) return false;
        if (PCMEMBER.equals(authority)) {
            return null != pcMemberRepository.findByUserAndPost(user, post);
        }
        return false;
    }


}
